﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Transactions;

public partial class Main_ServiceInvoice_CustomerPreference : System.Web.UI.Page
{
    Datautility du = new Datautility();
    Hashtable htchildID = new Hashtable();
    List<string> ControlCounter;
    //  List<string> ButtonID;
    List<string> ServiceID;
    List<string> RateID;
    List<string> UnitID;
    List<string> UOMID;
    List<string> Unit1ID;
    List<string> Unit2ID;
    List<string> Unit3ID;

    //int rowcountfordetails = 0;
    int rowcount = 0;
    int rdcount = 1;
    int billSelectedIndex = -1;

    bool UnitRate = false;
    bool UOM = false;
    bool unit1 = false;
    bool unit2 = false;
    bool unit3 = false;
    bool checkTax = false;

    string unit1MainCaption = "0";
    string unit1caption = "0";
    string unit2caption = "0";
    string unit3caption = "0";
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        OverrideRow();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (Request.QueryString["action"] != null)
        {
            if (Request.QueryString["action"].ToString() == "addnew")
            {
                pnlAddNew.Visible = true;
           
                if (!IsPostBack)
                {
                    ControlCounter = new List<string>();
                    ViewState["ControlCounter"] = ControlCounter;

                    //ButtonID = new List<string>();
                    //ViewState["ButtonID"] = ButtonID;

                    ServiceID = new List<string>();
                    ViewState["ServiceID"] = ServiceID;

                    RateID = new List<string>();
                    ViewState["RateID"] = RateID;

                    UnitID = new List<string>();
                    ViewState["UnitID"] = UnitID;

                    UOMID = new List<string>();
                    ViewState["UOMID"] = UOMID;

                    Unit1ID = new List<string>();
                    ViewState["Unit1ID"] = Unit1ID;

                    Unit2ID = new List<string>();
                    ViewState["Unit2ID"] = Unit2ID;

                    Unit3ID = new List<string>();
                    ViewState["Unit3ID"] = Unit3ID;

                    ViewState["checkTaxable"] = "0";
                    ViewState["unitrate"] = "0";
                    ViewState["UOM"] = "0";
                    ViewState["unit1"] = "0";
                    ViewState["unit2"] = "0";
                    ViewState["unit3"] = "0";
                    ViewState["unitCPMain"] = "0";
                    ViewState["unitCP1"] = "0";
                    ViewState["unitCP2"] = "0";
                    ViewState["unitCP3"] = "0";
                    ViewState["formatid"] = "0";
                    ViewState["custPreId"] = "0";
                    //fillGrid();
                    lblMsg.Text = "";
                    btnSave.Text = "Save";
                    btnAddMore.Visible = false;
                    fillBillFormat();
                    fillCustomerName();

                    if (Request.QueryString["msg"] != null)
                    {
                        if (Request.QueryString["msg"].ToString() == "IS")
                        {
                            lblMsg.Text = "Inserted Successfully !!";
                        }
                        else if (Request.QueryString["msg"].ToString() == "UP")
                        {
                            if (Request.QueryString["id"] != string.Empty)
                            {
                                pnlAddNew.Visible = true;
                                pnlView.Visible = false;
                                CheckPreAddedORUpdateNew(Request.QueryString["id"].ToString());
                                btnSave.Text = "Update";
                            }
                        }
                    }
                }
            }
            else
            {
                pnlView.Visible = true;
                fillGrid();
            }
        }
    }
    protected void fillCustomerName()
    {
        DataTable dtFillCustomer = new DataTable();
        dtFillCustomer = du.getdatausingprocedure("USP_IVC_GetCustomerLedger");
        if (dtFillCustomer.Rows.Count > 0)
        {
            ddlCustomer.DataValueField = "LID";
            ddlCustomer.DataTextField = "LNAME";
            ddlCustomer.DataSource = dtFillCustomer;
            ddlCustomer.DataBind();
            ddlCustomer.Items.Insert(0, "-Select Customer Name-");
            ddlCustomer.SelectedIndex = 0;
        }
        else
        {
            ddlCustomer.Items.Insert(0, "-No Customer Found-");
            ddlCustomer.SelectedIndex = 0;
        }
    }
    protected void fillBillFormat()
    {
        DataTable dtBillFormat = new DataTable();
        SqlParameter[] trantype =
        {
            new SqlParameter("@TRANSACTIONTYPE","1")
        };
        dtBillFormat = du.getdatausingprocedure("USP_IVC_GetAllTransactionFormatNameID", trantype);
        if (dtBillFormat.Rows.Count > 0)
        {
            ddlBillFormat.DataValueField = "FORMATID";
            ddlBillFormat.DataTextField = "FORMATNAME";
            ddlBillFormat.DataSource = dtBillFormat;
            ddlBillFormat.DataBind();
            ddlBillFormat.Items.Insert(0, "-Select Bill Format-");
            ddlBillFormat.SelectedIndex = 0;
        }
        else
        {
            ddlBillFormat.Items.Insert(0, "-No Item Found-");
            ddlBillFormat.SelectedIndex = 0;
        }


    }
    protected DataTable selectUnitdetails()
    {
        DataTable dtUnit = new DataTable();
        if (ddlBillFormat.SelectedIndex > 0)
        {
            SqlParameter[] dfds = {
                                  new SqlParameter("@FORMATID",ViewState["formatid"].ToString())
                              };
            dtUnit = du.getdatausingprocedure("USP_IVC_GetTransactionFormat_AdditionalUnits", dfds);
            tblDynamic.Visible = true;
            tableFirst.Visible = false;
            btnAddMore.Visible = true;
        }
        else
        {
            tblDynamic.Visible = false;
            tableFirst.Visible = true;
            btnAddMore.Visible = false;
        }
        return dtUnit;
    }
    protected DataTable checkUnitRateUOM()
    {
        DataTable dtCheck = new DataTable();
        if (ddlBillFormat.SelectedIndex > 0)
        {
            SqlParameter[] dfds = {
                                  new SqlParameter("@FORMATID",ViewState["formatid"].ToString())
                              };
            dtCheck = du.getdatausingprocedure("USP_IVC_GetTransactionFormat", dfds);
            tblDynamic.Visible = true;
            tableFirst.Visible = false;
            btnAddMore.Visible = true;
        }
        else
        {
            tblDynamic.Visible = false;
            tableFirst.Visible = true;
            btnAddMore.Visible = false;
        }
        return dtCheck;
    }
    protected DataTable checkUnitRateUOM(string strFormatId)
    {
        DataTable dtCheck = new DataTable();
        if (strFormatId != "0")
        {
            SqlParameter[] dfds = {
                                  new SqlParameter("@FORMATID",strFormatId)
                              };
            dtCheck = du.getdatausingprocedure("USP_IVC_GetTransactionFormat", dfds);
        }
        return dtCheck;
    }
    protected DataTable checkUOM(string strFormatId)
    {
        DataTable dtCheck = new DataTable();
        if (strFormatId != "0")
        {
            SqlParameter[] dfds = {
                                  new SqlParameter("@FORMATID",strFormatId)
                              };
            dtCheck = du.getdatausingprocedure("USP_IVC_GetTransactionFormat_AdditionalUnits", dfds);
        }
        return dtCheck;
    }
    protected string updateInParent(string custpreid)
    {
        try
        {
            int crdays = 0;
            if (txtCreditDays.Text.Trim() != "")
            {
                crdays = Convert.ToInt32(txtCreditDays.Text.Trim());
            }

            SqlParameter[] UpdParent = 
                                {
                                    new SqlParameter("@CUSTOMERPREID",custpreid),
                                    new SqlParameter("@CUSTOMERLEDGERID",ddlCustomer.SelectedValue),
                                    new SqlParameter("@FORMATID",ddlBillFormat.SelectedValue),      
                                    new SqlParameter("@CRDAYS",crdays)
                                };
            int result = du.executeprocedure("USP_IVC_SaveCustomerPreference", UpdParent);
            if (result > 0)
            {
                if (custpreid == "0")
                {
                    DataTable dtCustomerPreID = du.getdata("select IDENT_CURRENT('IVC_CUSTOMER_PREFERENCE_PARENT') as lastID");
                    custpreid = dtCustomerPreID.Rows[0]["lastID"].ToString();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return custpreid;
    }
    protected void updateInChild(string strParentId)
    {
        try
        {
            if (strParentId != string.Empty)
            {
                int count = Convert.ToInt32(hiddenControl.Value);
                decimal desTxtRate = 0.00M;
                decimal desTxtUnit = 0.00M;
                string strTxtUOM = "";
                decimal desTxtUnit2 = 0.00M;
                decimal desTxtUnit3 = 0.00M;
                decimal desTxtUnit4 = 0.00M;
                UnitRate = Convert.ToBoolean(ViewState["unitrate"]);
                UOM = Convert.ToBoolean(ViewState["UOM"]);
                unit1 = Convert.ToBoolean(ViewState["unit1"]);
                unit2 = Convert.ToBoolean(ViewState["unit2"]);
                unit3 = Convert.ToBoolean(ViewState["unit3"]);
                Response.Write("IN UPDATE CHILD");
                Hashtable htchildID = (Hashtable)Session["hashtable"];

                for (int i = 1; i <= count; i++)
                {
                    DropDownList ddlservice = (DropDownList)tblDynamic.Rows[i].FindControl("ddlService" + i.ToString());
                    if (UnitRate == true)
                    {
                        TextBox TxtRate = (TextBox)tblDynamic.Rows[i].FindControl("txtRate" + i.ToString());
                        TextBox TxtUnit = (TextBox)tblDynamic.Rows[i].FindControl("txtUnit" + i.ToString());
                        desTxtRate = Convert.ToDecimal(TxtRate.Text.Trim());
                        desTxtUnit = Convert.ToDecimal(TxtUnit.Text.Trim());
                    }
                    if (UOM == true)
                    {
                        TextBox TxtUOM = (TextBox)tblDynamic.Rows[i].FindControl("txtUOM" + i.ToString());
                        strTxtUOM = TxtUOM.Text.Trim();
                    }
                    if (unit1 == true)
                    {
                        TextBox TxtUnit2 = (TextBox)tblDynamic.Rows[i].FindControl("txtUnitFirst" + i.ToString());
                        desTxtUnit2 = Convert.ToDecimal(TxtUnit2.Text.Trim());
                    }
                    if (unit2 == true)
                    {
                        TextBox TxtUnit3 = (TextBox)tblDynamic.Rows[i].FindControl("txtUnitSecond" + i.ToString());
                        desTxtUnit3 = Convert.ToDecimal(TxtUnit3.Text.Trim());
                    }
                    if (unit3 == true)
                    {
                        TextBox TxtUnit4 = (TextBox)tblDynamic.Rows[i].FindControl("txtUnitThird" + i.ToString());
                        desTxtUnit4 = Convert.ToDecimal(TxtUnit4.Text.Trim());
                    }

                    SqlParameter[] UpdChild = 
                    {
                        new SqlParameter("@CUSTOMERPRECHILDID",htchildID[i].ToString()),
                        new SqlParameter("@CUSTOMERPREID",strParentId),
                        new SqlParameter("@SERVICEID",ddlservice.SelectedValue), 
                        new SqlParameter("@RATE",desTxtRate),
                        new SqlParameter("@UNIT",desTxtUnit),
                        new SqlParameter("@UNIT2",desTxtUnit2),
                        new SqlParameter("@UNIT3",desTxtUnit3),
                        new SqlParameter("@UNIT4",desTxtUnit4),
                        new SqlParameter("@UOM",strTxtUOM)
                    };

                    du.executeprocedure("USP_IVC_SaveCustomerPreferenceServices", UpdChild);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void fillServices(DropDownList ddl)
    {
        string strsql = "select SERVICEID, SERVICENAME from IVC_SERVICEMASTER";
        DataTable dtService = du.getdata(strsql);
        if (dtService.Rows.Count > 0)
        {
            ddl.DataTextField = "SERVICENAME";
            ddl.DataValueField = "SERVICEID";
            ddl.DataSource = dtService;
            ddl.DataBind();
            ddl.Items.Insert(0, "-select service-");
            ddl.SelectedIndex = 0;
        }
        else
        {
            ddl.Items.Insert(0, "-no service found-");
            ddl.SelectedIndex = 0;
        }

    }
    protected void fillGrid()
    {
        DataTable dtFillPref = du.getdatausingprocedure("USP_IVC_GetCustomerPrefrenceDetails");
        grdCustPref.DataSource = dtFillPref;
        grdCustPref.DataBind();
    }
    protected void fillViewStateforRowCreate()
    {
        DataTable dtCheck = checkUnitRateUOM();
        if (dtCheck.Rows.Count > 0)
        {
            ViewState["unitrate"] = Convert.ToBoolean(dtCheck.Rows[0]["UNITRATE"].ToString());
            ViewState["UOM"] = Convert.ToBoolean(dtCheck.Rows[0]["UOM"].ToString());
            ViewState["checkTaxable"] = Convert.ToBoolean(dtCheck.Rows[0]["NONTAXABLE"].ToString());
        }

        DataTable dtUnit = selectUnitdetails();
        if (dtUnit.Rows.Count > 0)
        {
            ViewState["unit1"] = Convert.ToBoolean(dtUnit.Rows[0]["UNIT1"].ToString());
            ViewState["unit2"] = Convert.ToBoolean(dtUnit.Rows[0]["UNIT2"].ToString());
            ViewState["unit3"] = Convert.ToBoolean(dtUnit.Rows[0]["UNIT3"].ToString());
            ViewState["unitCPMain"] = dtUnit.Rows[0]["UNIT1MAIN_CAPTION"].ToString();

            ViewState["unitCP1"] = dtUnit.Rows[0]["UNIT1_CAPTION"].ToString();
            ViewState["unitCP2"] = dtUnit.Rows[0]["UNIT2_CAPTION"].ToString();
            ViewState["unitCP3"] = dtUnit.Rows[0]["UNIT3_CAPTION"].ToString();
        }
        UnitRate = Convert.ToBoolean(Convert.ToInt32(ViewState["unitrate"]));
        UOM = Convert.ToBoolean(Convert.ToInt32(ViewState["UOM"]));
        checkTax = Convert.ToBoolean(Convert.ToInt32(ViewState["checkTaxable"]));

        unit1 = Convert.ToBoolean(Convert.ToInt32(ViewState["unit1"]));
        unit2 = Convert.ToBoolean(Convert.ToInt32(ViewState["unit2"]));
        unit3 = Convert.ToBoolean(Convert.ToInt32(ViewState["unit3"]));

        unit1MainCaption = ViewState["unitCPMain"].ToString();
        unit1caption = ViewState["unitCP1"].ToString();
        unit2caption = ViewState["unitCP2"].ToString();
        unit3caption = ViewState["unitCP3"].ToString();
    }
    protected void CheckPreAddedORUpdateNew(string id)
    {
        if (id != "")
        {
            DataTable dtFormatID = du.getdata("select customerledgerid,formatid,Crdays from IVC_CUSTOMER_PREFERENCE_PARENT where customerpreid='" + id + "'");

            SqlParameter[] custpreID ={
                                          new SqlParameter("@CUSTOMERPREID", id)
                                      };

            DataTable dtCustomerPrefChild = du.getdatausingprocedure("USP_IVC_GetCustomerPreferenceServices", custpreID);

            tblDynamic.Rows.Clear();
            ControlCounter = new List<string>();
            //ButtonID = new List<string>();
            //ViewState["ButtonID"] = ButtonID;

            ServiceID = new List<string>();
            ViewState["ServiceID"] = ServiceID;

            RateID = new List<string>();
            ViewState["RateID"] = RateID;

            UnitID = new List<string>();
            ViewState["UnitID"] = UnitID;

            Unit1ID = new List<string>();
            ViewState["Unit1ID"] = Unit1ID;

            Unit2ID = new List<string>();
            ViewState["Unit2ID"] = Unit2ID;

            Unit3ID = new List<string>();
            ViewState["Unit3ID"] = Unit3ID;

            UOMID = new List<string>();
            ViewState["UOMID"] = UOMID;
            ViewState["formatid"] = dtFormatID.Rows[0]["formatid"].ToString();
            ddlBillFormat.SelectedValue = dtFormatID.Rows[0]["formatid"].ToString();
            ddlCustomer.SelectedValue = dtFormatID.Rows[0]["customerledgerid"].ToString();
            ViewState["custPreId"] = id;
            txtCreditDays.Text = dtFormatID.Rows[0]["CRDAYS"].ToString();
            rowcount = 0;
            fillViewStateforRowCreate();
            int count = Convert.ToInt32(dtCustomerPrefChild.Rows.Count);

            for (int i = 1; i <= count; i++)
            {
                AddRow();
                DropDownList ddlservice = (DropDownList)tblDynamic.Rows[i].FindControl("ddlService" + i.ToString());
                ddlservice.SelectedValue = dtCustomerPrefChild.Rows[i - 1]["SERVICEID"].ToString();
                htchildID.Add(i, dtCustomerPrefChild.Rows[i - 1]["CUSTOMERPRECHILDID"].ToString());
                if (UnitRate == true)
                {
                    TextBox TxtRate = (TextBox)tblDynamic.Rows[i].FindControl("txtRate" + i.ToString());
                    TextBox TxtUnit = (TextBox)tblDynamic.Rows[i].FindControl("txtUnit" + i.ToString());
                    TxtRate.Text = dtCustomerPrefChild.Rows[i - 1]["RATE"].ToString();
                    TxtUnit.Text = dtCustomerPrefChild.Rows[i - 1]["UNIT"].ToString();
                }
                if (UOM == true)
                {
                    TextBox TxtUOM = (TextBox)tblDynamic.Rows[i].FindControl("txtUOM" + i.ToString());
                    TxtUOM.Text = dtCustomerPrefChild.Rows[i - 1]["UOM"].ToString();
                }
                if (unit1 == true)
                {
                    TextBox TxtUnit2 = (TextBox)tblDynamic.Rows[i].FindControl("txtUnitFirst" + i.ToString());
                    TxtUnit2.Text = dtCustomerPrefChild.Rows[i - 1]["UNIT2"].ToString();
                }
                if (unit2 == true)
                {
                    TextBox TxtUnit3 = (TextBox)tblDynamic.Rows[i].FindControl("txtUnitSecond" + i.ToString());
                    TxtUnit3.Text = dtCustomerPrefChild.Rows[i - 1]["UNIT3"].ToString();
                }
                if (unit3 == true)
                {
                    TextBox TxtUnit4 = (TextBox)tblDynamic.Rows[i].FindControl("txtUnitThird" + i.ToString());
                    TxtUnit4.Text = dtCustomerPrefChild.Rows[i - 1]["UNIT4"].ToString();
                }
            }
            Session["hashtable"] = htchildID;
        }
    }
    protected void AddRow()
    {
        rowcount++;
        ControlCounter.Add(rowcount.ToString());
        if (rowcount <= 1 || rdcount <= 0)
        {
            HtmlTableRow rowFirst = new HtmlTableRow();
            rowFirst.ID = "rowFirst" + rowcount.ToString();
            HtmlTableCell CellFirst1 = new HtmlTableCell();
            //CellFirst1.ColSpan = 2;
            CellFirst1.Attributes["class"] = "headtd";
            CellFirst1.Width = Unit.Percentage(20).ToString();


            HtmlTableCell CellFirst2 = new HtmlTableCell();
            CellFirst2.ColSpan = 3;
            CellFirst2.Attributes["class"] = "headtd";
            CellFirst2.Width = Unit.Percentage(40).ToString();

            HtmlTableCell CellFirst3 = new HtmlTableCell();
            CellFirst3.ColSpan = 3;
            CellFirst3.Attributes["class"] = "headtd";
            CellFirst3.Attributes["style"] = "padding-left:16%";
            CellFirst3.Width = "auto";

            Literal ltFirstUnit1MainCaption = new Literal();
            ltFirstUnit1MainCaption.Text = unit1MainCaption;
            CellFirst3.Controls.Add(ltFirstUnit1MainCaption);
            rowFirst.Cells.Add(CellFirst1);
            rowFirst.Cells.Add(CellFirst2);
            rowFirst.Cells.Add(CellFirst3);
            tblDynamic.Rows.Add(rowFirst);

            HtmlTableRow htrowHeading = new HtmlTableRow();
            htrowHeading.ID = "htrowHeading" + rowcount.ToString();
            HtmlTableCell htheader1 = new HtmlTableCell();
            htheader1.Width = Unit.Percentage(20).ToString();
            htheader1.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px;";
            Literal ltService = new Literal();
            ltService.Text = "Service";
            htheader1.Controls.Add(ltService);
            htrowHeading.Cells.Add(htheader1);

            //HtmlTableCell htheader2 = new HtmlTableCell();
            //htheader2.Width = "2%";
            //htheader2.Attributes["class"] = "headtd";
            //Literal ltButton = new Literal();
            //ltButton.Text = "...";
            //htheader2.Controls.Add(ltButton);
            //htrowHeading.Cells.Add(htheader2);
            if (UnitRate == true)
            {
                HtmlTableCell htheader3 = new HtmlTableCell();
                htheader3.Width = Unit.Percentage(13).ToString();
                htheader3.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px;";
                Literal ltRate = new Literal();
                ltRate.Text = "Rate";
                htheader3.Controls.Add(ltRate);
                htrowHeading.Cells.Add(htheader3);

                HtmlTableCell htheader4 = new HtmlTableCell();
                htheader4.Width = Unit.Percentage(13).ToString();
                htheader4.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px;";
                Literal ltUnit = new Literal();
                ltUnit.Text = "Unit";
                htheader4.Controls.Add(ltUnit);
                htrowHeading.Cells.Add(htheader4);
            }
            if (UOM == true)
            {
                HtmlTableCell htheader5 = new HtmlTableCell();
                htheader5.Width = Unit.Percentage(14).ToString();
                htheader5.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px; ";
                Literal ltUOM = new Literal();
                ltUOM.Text = "UOM";
                htheader5.Controls.Add(ltUOM);
                htrowHeading.Cells.Add(htheader5);
            }
            if (unit1 == true)
            {
                HtmlTableCell htheader6 = new HtmlTableCell();
                htheader6.Width = Unit.Percentage(13).ToString();
                htheader6.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px; ";
                Literal ltUnit1 = new Literal();
                ltUnit1.Text = unit1caption;
                htheader6.Controls.Add(ltUnit1);
                htrowHeading.Cells.Add(htheader6);
            }

            if (unit2 == true)
            {
                HtmlTableCell htheader7 = new HtmlTableCell();
                htheader7.Width = Unit.Percentage(13).ToString();
                htheader7.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px;";
                Literal ltUnit2 = new Literal();
                ltUnit2.Text = unit2caption;
                htheader7.Controls.Add(ltUnit2);
                htrowHeading.Cells.Add(htheader7);
            }

            if (unit3 == true)
            {
                HtmlTableCell htheader8 = new HtmlTableCell();
                htheader8.Width = Unit.Percentage(14).ToString();
                htheader8.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px;";
                Literal ltUnit3 = new Literal();
                ltUnit3.Text = unit3caption;
                htheader8.Controls.Add(ltUnit3);
                htrowHeading.Cells.Add(htheader8);
            }
            tblDynamic.Rows.Add(htrowHeading);
        }
        HtmlTableRow htrow = new HtmlTableRow();
        htrow.ID = "htrow" + rowcount.ToString();
        HtmlTableCell htcell1 = new HtmlTableCell();
        htcell1.ID = "htcell1" + rowcount.ToString();
        htcell1.Align = "top";
        htcell1.Width = Unit.Percentage(20).ToString();


        DropDownList ddlService = new DropDownList();
        ddlService.ID = "ddlService" + rowcount.ToString();
        ddlService.ToolTip = "service";
        ddlService.Width = Unit.Percentage(96);
        fillServices(ddlService);
        ddlService.AutoPostBack = true;
        ddlService.Attributes.Add("onchange", "checkCustomer(this.id);NoServiceRepeat(this.id);");
        htcell1.Controls.Add(ddlService);
        ServiceID.Add(ddlService.ID);
        htrow.Cells.Add(htcell1);

        //HtmlTableCell htcell2 = new HtmlTableCell();
        //htcell2.ID = "htcell2" + rowcount.ToString();
        //htcell2.Align = "Top";
        //htcell2.Width = "2%";

        //Button btnServiceMaster = new Button();
        //btnServiceMaster.ID = "btnServiceMaster" + rowcount.ToString();
        //btnServiceMaster.Text = "...";
        //btnServiceMaster.ToolTip = "Service Master";
        //btnServiceMaster.CssClass = "button";
        //btnServiceMaster.Width = Unit.Percentage(96);
        //btnServiceMaster.Attributes.Add("onclick", "openServiceMaster();");
        //htcell2.Controls.Add(btnServiceMaster);
        //ButtonID.Add(btnServiceMaster.ID);
        //htrow.Cells.Add(htcell2);

        if (UnitRate == true)
        {
            HtmlTableCell htcell3 = new HtmlTableCell();
            htcell3.ID = "htcell3" + rowcount.ToString();
            htcell3.Align = "top";
            htcell3.Width = Unit.Percentage(13).ToString();

            TextBox txtRate = new TextBox();
            txtRate.ID = "txtRate" + rowcount.ToString();
            txtRate.ToolTip = "Rate";
            txtRate.CssClass = "inputnumber";
            txtRate.MaxLength = 7;
            txtRate.Width = Unit.Percentage(95);
            txtRate.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
            if (checkTax == true)
            {
                txtRate.Enabled = false;
                txtRate.Text = "0";
            }
            htcell3.Controls.Add(txtRate);
            RateID.Add(txtRate.ID);
            htrow.Cells.Add(htcell3);

            HtmlTableCell htcell4 = new HtmlTableCell();
            htcell4.ID = "htcell4" + rowcount.ToString();
            htcell4.Align = "top";
            htcell4.Width = Unit.Percentage(13).ToString();

            TextBox txtUnit = new TextBox();
            txtUnit.ID = "txtUnit" + rowcount.ToString();
            txtUnit.ToolTip = "Unit";
            txtUnit.CssClass = "inputnumber";
            txtUnit.MaxLength = 7;
            txtUnit.Width = Unit.Percentage(95);
            txtUnit.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
            htcell4.Controls.Add(txtUnit);
            UnitID.Add(txtUnit.ID);
            htrow.Cells.Add(htcell4);
        }
        if (UOM == true)
        {
            HtmlTableCell htcell5 = new HtmlTableCell();
            htcell5.ID = "htcell5" + rowcount.ToString();
            htcell5.Align = "top";
            htcell5.Width = Unit.Percentage(14).ToString();

            TextBox txtUOM = new TextBox();
            txtUOM.ID = "txtUOM" + rowcount.ToString();
            txtUOM.ToolTip = "Unit of Measurement";
            txtUOM.CssClass = "inputnumber";
            txtUOM.MaxLength = 20;
            txtUOM.Width = Unit.Percentage(95);
            htcell5.Controls.Add(txtUOM);
            UOMID.Add(txtUOM.ID);
            htrow.Cells.Add(htcell5);
        }
        if (unit1 == true)
        {
            HtmlTableCell htcell6 = new HtmlTableCell();
            htcell6.ID = "htcell6" + rowcount.ToString();
            htcell6.Align = "top";
            htcell6.Width = Unit.Percentage(13).ToString();

            TextBox txtUnitFirst = new TextBox();
            txtUnitFirst.ID = "txtUnitFirst" + rowcount.ToString();
            txtUnitFirst.ToolTip = "Unit First";
            txtUnitFirst.CssClass = "inputnumber";
            txtUnitFirst.MaxLength = 7;
            txtUnitFirst.Width = Unit.Percentage(95);
            txtUnitFirst.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
            htcell6.Controls.Add(txtUnitFirst);
            Unit1ID.Add(txtUnitFirst.ID);
            htrow.Cells.Add(htcell6);
        }
        if (unit2 == true)
        {
            HtmlTableCell htcell7 = new HtmlTableCell();
            htcell7.ID = "htcell7" + rowcount.ToString();
            htcell7.Align = "top";
            htcell7.Width = Unit.Percentage(13).ToString();

            TextBox txtUnitSecond = new TextBox();
            txtUnitSecond.ID = "txtUnitSecond" + rowcount.ToString();
            txtUnitSecond.ToolTip = "Unit Second";
            txtUnitSecond.CssClass = "inputnumber";
            txtUnitSecond.MaxLength = 7;
            txtUnitSecond.Width = Unit.Percentage(95);
            txtUnitSecond.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
            htcell7.Controls.Add(txtUnitSecond);
            Unit2ID.Add(txtUnitSecond.ID);
            htrow.Cells.Add(htcell7);
        }
        if (unit3 == true)
        {
            HtmlTableCell htcell8 = new HtmlTableCell();
            htcell8.ID = "htcell8" + rowcount.ToString();
            htcell8.Align = "top";
            htcell8.Width = Unit.Percentage(14).ToString();

            TextBox txtUnitThird = new TextBox();
            txtUnitThird.ID = "txtUnitThird" + rowcount.ToString();
            txtUnitThird.ToolTip = "Unit Third";
            txtUnitThird.CssClass = "inputnumber";
            txtUnitThird.MaxLength = 7;
            txtUnitThird.Width = Unit.Percentage(95);
            txtUnitThird.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
            htcell8.Controls.Add(txtUnitThird);
            Unit3ID.Add(txtUnitThird.ID);
            htrow.Cells.Add(htcell8);
        }
        tblDynamic.Rows.Add(htrow);

        ViewState["ControlCounter"] = ControlCounter;
        ViewState["ServicID"] = ServiceID;
        //  ViewState["ButtonID"] = ButtonID;
        ViewState["RateID"] = RateID;
        ViewState["UnitID"] = UnitID;
        ViewState["UOMID"] = UOMID;
        ViewState["Unit1ID"] = Unit1ID;
        ViewState["Unit2ID"] = Unit2ID;
        ViewState["Unit3ID"] = Unit3ID;
        hiddenControl.Value = rowcount.ToString();
    }
    protected void OverrideRow()
    {
        ControlCounter = (List<string>)ViewState["ControlCounter"];
        ServiceID = (List<string>)ViewState["ServiceID"];
        // ButtonID = (List<string>)ViewState["ButtonID"];
        RateID = (List<string>)ViewState["RateID"];
        UnitID = (List<string>)ViewState["UnitID"];
        Unit1ID = (List<string>)ViewState["Unit1ID"];
        Unit2ID = (List<string>)ViewState["Unit2ID"];
        Unit3ID = (List<string>)ViewState["Unit3ID"];
        UOMID = (List<string>)ViewState["UOMID"];

        if (ControlCounter != null)
        {
            int count = Convert.ToInt32(ControlCounter.Count);

            for (int i = 0; i < count; i++)
            {
                rowcount++;
                UnitRate = Convert.ToBoolean(Convert.ToInt32(ViewState["unitrate"]));
                UOM = Convert.ToBoolean(Convert.ToInt32(ViewState["UOM"]));
                checkTax = Convert.ToBoolean(Convert.ToInt32(ViewState["checkTaxable"]));

                unit1 = Convert.ToBoolean(Convert.ToInt32(ViewState["unit1"]));
                unit2 = Convert.ToBoolean(Convert.ToInt32(ViewState["unit2"]));
                unit3 = Convert.ToBoolean(Convert.ToInt32(ViewState["unit3"]));

                unit1MainCaption = ViewState["unitCPMain"].ToString();
                unit1caption = ViewState["unitCP1"].ToString();
                unit2caption = ViewState["unitCP2"].ToString();
                unit3caption = ViewState["unitCP3"].ToString();
                if (rowcount <= 1 || rdcount <= 0)
                {
                    HtmlTableRow rowFirst = new HtmlTableRow();
                    HtmlTableCell CellFirst1 = new HtmlTableCell();
                    //CellFirst1.ColSpan = 2;
                    CellFirst1.Attributes["class"] = "headtd";
                    CellFirst1.Width = Unit.Percentage(20).ToString();

                    HtmlTableCell CellFirst2 = new HtmlTableCell();
                    CellFirst2.ColSpan = 3;
                    CellFirst2.Attributes["class"] = "headtd";
                    CellFirst2.Width = Unit.Percentage(40).ToString();

                    HtmlTableCell CellFirst3 = new HtmlTableCell();
                    CellFirst3.ColSpan = 3;
                    CellFirst3.Attributes["class"] = "headtd";
                    CellFirst3.Attributes["style"] = "padding-left:16%";
                    CellFirst3.Width = "auto";

                    Literal ltFirstUnit1MainCaption = new Literal();
                    ltFirstUnit1MainCaption.Text = unit1MainCaption;
                    CellFirst3.Controls.Add(ltFirstUnit1MainCaption);
                    rowFirst.Cells.Add(CellFirst1);
                    rowFirst.Cells.Add(CellFirst2);
                    rowFirst.Cells.Add(CellFirst3);
                    tblDynamic.Rows.Add(rowFirst);

                    HtmlTableRow htrowHeading = new HtmlTableRow();

                    HtmlTableCell htheader1 = new HtmlTableCell();
                    htheader1.Width = Unit.Percentage(20).ToString();
                    htheader1.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px;";
                    Literal ltService = new Literal();
                    ltService.Text = "Service";
                    htheader1.Controls.Add(ltService);
                    htrowHeading.Cells.Add(htheader1);

                    //HtmlTableCell htheader2 = new HtmlTableCell();
                    //htheader2.Width = "2%";
                    //htheader2.Attributes["class"] = "headtd";
                    //Literal ltButton = new Literal();
                    //ltButton.Text = "...";
                    //htheader2.Controls.Add(ltButton);
                    //htrowHeading.Cells.Add(htheader2);
                    if (UnitRate == true)
                    {
                        HtmlTableCell htheader3 = new HtmlTableCell();
                        htheader3.Width = Unit.Percentage(13).ToString();
                        htheader3.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px;";
                        Literal ltRate = new Literal();
                        ltRate.Text = "Rate";
                        htheader3.Controls.Add(ltRate);
                        htrowHeading.Cells.Add(htheader3);

                        HtmlTableCell htheader4 = new HtmlTableCell();
                        htheader4.Width = Unit.Percentage(13).ToString();
                        htheader4.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px;";
                        Literal ltUnit = new Literal();
                        ltUnit.Text = "Unit";
                        htheader4.Controls.Add(ltUnit);
                        htrowHeading.Cells.Add(htheader4);
                    }
                    if (UOM == true)
                    {
                        HtmlTableCell htheader5 = new HtmlTableCell();
                        htheader5.Width = Unit.Percentage(14).ToString();
                        htheader5.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px;";
                        Literal ltUOM = new Literal();
                        ltUOM.Text = "UOM";
                        htheader5.Controls.Add(ltUOM);
                        htrowHeading.Cells.Add(htheader5);
                    }
                    if (unit1 == true)
                    {
                        HtmlTableCell htheader6 = new HtmlTableCell();
                        htheader6.Width = Unit.Percentage(13).ToString();
                        htheader6.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px;";
                        Literal ltUnit1 = new Literal();
                        ltUnit1.Text = unit1caption;
                        htheader6.Controls.Add(ltUnit1);
                        htrowHeading.Cells.Add(htheader6);
                    }

                    if (unit2 == true)
                    {
                        HtmlTableCell htheader7 = new HtmlTableCell();
                        htheader7.Width = Unit.Percentage(13).ToString();
                        htheader7.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px;";
                        Literal ltUnit2 = new Literal();
                        ltUnit2.Text = unit2caption;
                        htheader7.Controls.Add(ltUnit2);
                        htrowHeading.Cells.Add(htheader7);
                    }

                    if (unit3 == true)
                    {
                        HtmlTableCell htheader8 = new HtmlTableCell();
                        htheader8.Width = Unit.Percentage(14).ToString();
                        htheader8.Attributes["style"] = "font-weight:bold; color:Black; font-size:11px;";
                        Literal ltUnit3 = new Literal();
                        ltUnit3.Text = unit3caption;
                        htheader8.Controls.Add(ltUnit3);
                        htrowHeading.Cells.Add(htheader8);
                    }
                    tblDynamic.Rows.Add(htrowHeading);
                }
                HtmlTableRow htrow = new HtmlTableRow();

                HtmlTableCell htcell1 = new HtmlTableCell();
                htcell1.ID = "htcell1" + rowcount.ToString();
                htcell1.Align = "top";
                htcell1.Width = Unit.Percentage(20).ToString();

                DropDownList ddlService = new DropDownList();
                ddlService.ID = ServiceID[i].ToString();
                ddlService.ToolTip = "service";
                ddlService.Width = Unit.Percentage(95);
                fillServices(ddlService);
                ddlService.AutoPostBack = true;
                ddlService.Attributes.Add("onchange", "checkCustomer(this.id);NoServiceRepeat(this.id);");
                //ddlService.Attributes.Add("onchange", "NoServiceRepeat(this.id);");
                htcell1.Controls.Add(ddlService);
                htrow.Cells.Add(htcell1);

                //HtmlTableCell htcell2 = new HtmlTableCell();
                //htcell2.ID = "htcell2" + rowcount.ToString();
                //htcell2.Align = "Top";
                //htcell2.Width = "2%";

                //Button btnServiceMaster = new Button();
                //btnServiceMaster.ID = ButtonID[i].ToString();
                //btnServiceMaster.Text = "...";
                //btnServiceMaster.ToolTip = "Service Master";
                //btnServiceMaster.CssClass = "button";
                //btnServiceMaster.Width = Unit.Percentage(96);
                //btnServiceMaster.Attributes.Add("onclick", "openServiceMaster();");
                //htcell2.Controls.Add(btnServiceMaster);
                // htrow.Cells.Add(htcell2);

                if (UnitRate == true)
                {
                    HtmlTableCell htcell3 = new HtmlTableCell();
                    htcell3.ID = "htcell3" + rowcount.ToString();
                    htcell3.Align = "top";
                    htcell3.Width = Unit.Percentage(13).ToString();

                    TextBox txtRate = new TextBox();
                    txtRate.ID = RateID[i].ToString();
                    txtRate.ToolTip = "Rate";
                    txtRate.CssClass = "inputnumber";
                    txtRate.MaxLength = 7;
                    txtRate.Width = Unit.Percentage(95);
                    txtRate.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
                    if (checkTax == true)
                    {
                        txtRate.Enabled = false;
                        txtRate.Text = "0";
                    }
                    htcell3.Controls.Add(txtRate);

                    htrow.Cells.Add(htcell3);

                    HtmlTableCell htcell4 = new HtmlTableCell();
                    htcell4.ID = "htcell4" + rowcount.ToString();
                    htcell4.Align = "top";
                    htcell4.Width = Unit.Percentage(13).ToString(); ;

                    TextBox txtUnit = new TextBox();
                    txtUnit.ID = UnitID[i].ToString();
                    txtUnit.ToolTip = "Unit";
                    txtUnit.CssClass = "inputnumber";
                    txtUnit.MaxLength = 7;
                    txtUnit.Width = Unit.Percentage(95);
                    txtUnit.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
                    htcell4.Controls.Add(txtUnit);

                    htrow.Cells.Add(htcell4);
                }
                if (UOM == true)
                {
                    HtmlTableCell htcell5 = new HtmlTableCell();
                    htcell5.ID = "htcell5" + rowcount.ToString();
                    htcell5.Align = "top";
                    htcell5.Width = Unit.Percentage(14).ToString(); ;

                    TextBox txtUOM = new TextBox();
                    txtUOM.ID = UOMID[i].ToString();
                    txtUOM.ToolTip = "Unit of Measurement";
                    txtUOM.CssClass = "inputnumber";
                    txtUOM.MaxLength = 20;
                    txtUOM.Width = Unit.Percentage(95);
                    htcell5.Controls.Add(txtUOM);
                    htrow.Cells.Add(htcell5);
                }
                if (unit1 == true)
                {
                    HtmlTableCell htcell6 = new HtmlTableCell();
                    htcell6.ID = "htcell6" + rowcount.ToString();
                    htcell6.Align = "top";
                    htcell6.Width = Unit.Percentage(13).ToString(); ;

                    TextBox txtUnitFirst = new TextBox();
                    txtUnitFirst.ID = Unit1ID[i].ToString();
                    txtUnitFirst.ToolTip = "Unit First";
                    txtUnitFirst.CssClass = "inputnumber";
                    txtUnitFirst.MaxLength = 7;
                    txtUnitFirst.Width = Unit.Percentage(95);
                    txtUnitFirst.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
                    htcell6.Controls.Add(txtUnitFirst);

                    htrow.Cells.Add(htcell6);
                }
                if (unit2 == true)
                {
                    HtmlTableCell htcell7 = new HtmlTableCell();
                    htcell7.ID = "htcell7" + rowcount.ToString();
                    htcell7.Align = "top";
                    htcell7.Width = Unit.Percentage(13).ToString(); ;

                    TextBox txtUnitSecond = new TextBox();
                    txtUnitSecond.ID = Unit2ID[i].ToString();
                    txtUnitSecond.ToolTip = "Unit Second";
                    txtUnitSecond.CssClass = "inputnumber";
                    txtUnitSecond.MaxLength = 7;
                    txtUnitSecond.Width = Unit.Percentage(95);
                    txtUnitSecond.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
                    htcell7.Controls.Add(txtUnitSecond);

                    htrow.Cells.Add(htcell7);
                }
                if (unit3 == true)
                {
                    HtmlTableCell htcell8 = new HtmlTableCell();
                    htcell8.ID = "htcell8" + rowcount.ToString();
                    htcell8.Align = "top";
                    htcell8.Width = Unit.Percentage(14).ToString(); ;

                    TextBox txtUnitThird = new TextBox();
                    txtUnitThird.ID = Unit3ID[i].ToString();
                    txtUnitThird.ToolTip = "Unit Third";
                    txtUnitThird.CssClass = "inputnumber";
                    txtUnitThird.MaxLength = 7;
                    txtUnitThird.Width = Unit.Percentage(95);
                    txtUnitThird.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
                    htcell8.Controls.Add(txtUnitThird);

                    htrow.Cells.Add(htcell8);
                }
                tblDynamic.Rows.Add(htrow);
            }

        }
    }


    protected DataTable CreateTableForDetail(string strCustomerPreId, string strFormat)
    {
        SqlParameter[] sparam ={
                                  new SqlParameter("@CUSTOMERPREID",strCustomerPreId)
                               };
        DataTable dtCustomerPrefChild = du.getdatausingprocedure("USP_IVC_GetCustomerPreferenceServices", sparam);

        DataTable DTMain = new DataTable();
        DataTable DtCheck = checkUnitRateUOM(strFormat);
        DataTable DTAddUnit = checkUOM(strFormat);
        if (dtCustomerPrefChild.Rows.Count > 0)
        {    
            for (int i = 0; i < dtCustomerPrefChild.Rows.Count; i++)
            {
                if (DTMain.Columns.Contains("Service_Name") == false)
                {
                    DTMain.Columns.Add("Service_Name");
                }
                DataRow DrMain = DTMain.NewRow();
                DrMain["Service_Name"] = dtCustomerPrefChild.Rows[i]["SERVICENAME"].ToString();
                if (Convert.ToBoolean(DtCheck.Rows[0]["UNITRATE"]) == true)
                {
                    if (DTMain.Columns.Contains("Rate") == false)
                    {
                        DTMain.Columns.Add("Rate");
                    }
                    DrMain["Rate"] = dtCustomerPrefChild.Rows[i]["RATE"].ToString();
                    if (DTMain.Columns.Contains("Unit") == false)
                    {
                        DTMain.Columns.Add("Unit");
                    }
                    DrMain["Unit"] = dtCustomerPrefChild.Rows[i]["UNIT"].ToString();
                }
                if (Convert.ToBoolean(DtCheck.Rows[0]["UOM"]) == true)
                {
                    if (DTMain.Columns.Contains("UOM") == false)
                    {
                        DTMain.Columns.Add("UOM");
                    }
                    DrMain["UOM"] = dtCustomerPrefChild.Rows[i]["UOM"].ToString();
                }
                if (Convert.ToBoolean(DTAddUnit.Rows[0]["UNIT1"]) == true)
                { 
                    string caption = DTAddUnit.Rows[0]["UNIT1_CAPTION"].ToString();
                    if (DTMain.Columns.Contains(caption) == false)
                    {
                        DTMain.Columns.Add(caption, typeof(string));
                    }
                    DrMain[caption] = dtCustomerPrefChild.Rows[i]["UNIT2"].ToString();
                }
                if (Convert.ToBoolean(DTAddUnit.Rows[0]["UNIT2"]) == true)
                {
                    string caption = DTAddUnit.Rows[0]["UNIT2_CAPTION"].ToString();
                    if (DTMain.Columns.Contains(caption) == false)
                    {
                        DTMain.Columns.Add(caption, typeof(string));
                    }
                    DrMain[caption] = dtCustomerPrefChild.Rows[i]["UNIT3"].ToString();
                }
                if (Convert.ToBoolean(DTAddUnit.Rows[0]["UNIT3"]) == true)
                {
                    string caption = DTAddUnit.Rows[0]["UNIT3_CAPTION"].ToString();
                    if (DTMain.Columns.Contains(caption) == false)
                    {
                        DTMain.Columns.Add(caption, typeof(string));
                    }
                    DrMain[caption] = dtCustomerPrefChild.Rows[i]["UNIT4"].ToString();
                }
                DTMain.Rows.Add(DrMain);
            }
        }
        return DTMain;
    }
    protected void FillViewStateForDetails()
    {
        DataTable dtUnit = new DataTable();
        SqlParameter[] dfds = {
                                  new SqlParameter("@FORMATID",ViewState["formatid"].ToString())
                              };
        dtUnit = du.getdatausingprocedure("USP_IVC_GetTransactionFormat_AdditionalUnits", dfds);

        DataTable dtCheck = new DataTable();
        SqlParameter[] dfds2 = {
                                  new SqlParameter("@FORMATID",ViewState["formatid"].ToString())
                              };
        dtCheck = du.getdatausingprocedure("USP_IVC_GetTransactionFormat", dfds2);

        if (dtCheck.Rows.Count > 0)
        {
            ViewState["unitrate"] = Convert.ToBoolean(dtCheck.Rows[0]["UNITRATE"].ToString());
            ViewState["UOM"] = Convert.ToBoolean(dtCheck.Rows[0]["UOM"].ToString());
            ViewState["checkTaxable"] = Convert.ToBoolean(dtCheck.Rows[0]["NONTAXABLE"].ToString());
        }

        if (dtUnit.Rows.Count > 0)
        {
            ViewState["unit1"] = Convert.ToBoolean(dtUnit.Rows[0]["UNIT1"].ToString());
            ViewState["unit2"] = Convert.ToBoolean(dtUnit.Rows[0]["UNIT2"].ToString());
            ViewState["unit3"] = Convert.ToBoolean(dtUnit.Rows[0]["UNIT3"].ToString());
            ViewState["unitCPMain"] = dtUnit.Rows[0]["UNIT1MAIN_CAPTION"].ToString();

            ViewState["unitCP1"] = dtUnit.Rows[0]["UNIT1_CAPTION"].ToString();
            ViewState["unitCP2"] = dtUnit.Rows[0]["UNIT2_CAPTION"].ToString();
            ViewState["unitCP3"] = dtUnit.Rows[0]["UNIT3_CAPTION"].ToString();
        }

        UnitRate = Convert.ToBoolean(Convert.ToInt32(ViewState["unitrate"]));
        UOM = Convert.ToBoolean(Convert.ToInt32(ViewState["UOM"]));
        checkTax = Convert.ToBoolean(Convert.ToInt32(ViewState["checkTaxable"]));

        unit1 = Convert.ToBoolean(Convert.ToInt32(ViewState["unit1"]));
        unit2 = Convert.ToBoolean(Convert.ToInt32(ViewState["unit2"]));
        unit3 = Convert.ToBoolean(Convert.ToInt32(ViewState["unit3"]));

        unit1MainCaption = ViewState["unitCPMain"].ToString();
        unit1caption = ViewState["unitCP1"].ToString();
        unit2caption = ViewState["unitCP2"].ToString();
        unit3caption = ViewState["unitCP3"].ToString();
    }

    protected void ddlBillFormat_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCustomer.SelectedIndex > 0 && ddlBillFormat.SelectedIndex > 0)
        {
            string strsql = "select customerpreid,customerledgerid,formatid from IVC_CUSTOMER_PREFERENCE_PARENT where customerledgerid='" + ddlCustomer.SelectedValue + "'and  formatid='" + ddlBillFormat.SelectedValue + "'";
            DataTable dtCheckPre = du.getdata(strsql);
            if (dtCheckPre.Rows.Count == 0)
            {
                tblDynamic.Rows.Clear();
                ControlCounter = new List<string>();
                //ButtonID = new List<string>();
                //ViewState["ButtonID"] = ButtonID;

                ServiceID = new List<string>();
                ViewState["ServiceID"] = ServiceID;

                RateID = new List<string>();
                ViewState["RateID"] = RateID;

                UnitID = new List<string>();
                ViewState["UnitID"] = UnitID;

                Unit1ID = new List<string>();
                ViewState["Unit1ID"] = Unit1ID;

                Unit2ID = new List<string>();
                ViewState["Unit2ID"] = Unit2ID;

                Unit3ID = new List<string>();
                ViewState["Unit3ID"] = Unit3ID;

                UOMID = new List<string>();
                ViewState["UOMID"] = UOMID;
                ViewState["formatid"] = ddlBillFormat.SelectedValue.ToString();

                rdcount = 0;
                rowcount = 0;
                fillViewStateforRowCreate();
                AddRow();
                btnSave.Text = "Save";
            }
            else
            {
                CheckPreAddedORUpdateNew(dtCheckPre.Rows[0]["customerpreid"].ToString());
                ViewState["custPreId"] = dtCheckPre.Rows[0]["customerpreid"].ToString();
                btnSave.Text = "Update";
            }
        }
    }
    protected void btnAddMore_Click(object sender, EventArgs e)
    {
        AddRow();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (tblDynamic.Visible)
            {
                if (btnSave.Text == "Save")
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        string strParentId = updateInParent("0");
                        int count = Convert.ToInt32(hiddenControl.Value);
                        decimal desTxtRate = 0.00M;
                        decimal desTxtUnit = 0.00M;
                        string strTxtUOM = "";
                        decimal desTxtUnit2 = 0.00M;
                        decimal desTxtUnit3 = 0.00M;
                        decimal desTxtUnit4 = 0.00M;
                        UnitRate = Convert.ToBoolean(ViewState["unitrate"].ToString());
                        UOM = Convert.ToBoolean(ViewState["UOM"].ToString());
                        unit1 = Convert.ToBoolean(ViewState["unit1"].ToString());
                        unit2 = Convert.ToBoolean(ViewState["unit2"].ToString());
                        unit3 = Convert.ToBoolean(ViewState["unit3"].ToString());

                        for (int i = 1; i <= count; i++)
                        {
                            DropDownList ddlservice = (DropDownList)tblDynamic.Rows[i].FindControl("ddlService" + i.ToString());
                            if (UnitRate == true)
                            {
                                TextBox TxtRate = (TextBox)tblDynamic.Rows[i].FindControl("txtRate" + i.ToString());
                                TextBox TxtUnit = (TextBox)tblDynamic.Rows[i].FindControl("txtUnit" + i.ToString());
                                if (TxtRate.Text == "")
                                {
                                    TxtRate.Text = "0";
                                }
                                if (TxtUnit.Text == "")
                                {
                                    TxtUnit.Text = "0";
                                }
                                desTxtRate = Convert.ToDecimal(TxtRate.Text);
                                desTxtUnit = Convert.ToDecimal(TxtUnit.Text);
                            }
                            if (UOM == true)
                            {
                                TextBox TxtUOM = (TextBox)tblDynamic.Rows[i].FindControl("txtUOM" + i.ToString());
                                strTxtUOM = TxtUOM.Text;
                            }
                            if (unit1 == true)
                            {
                                TextBox TxtUnit2 = (TextBox)tblDynamic.Rows[i].FindControl("txtUnitFirst" + i.ToString());
                                if (TxtUnit2.Text == "")
                                {
                                    TxtUnit2.Text = "0";
                                }
                                desTxtUnit2 = Convert.ToDecimal(TxtUnit2.Text);
                            }
                            if (unit2 == true)
                            {
                                TextBox TxtUnit3 = (TextBox)tblDynamic.Rows[i].FindControl("txtUnitSecond" + i.ToString());
                                if (TxtUnit3.Text == "")
                                {
                                    TxtUnit3.Text = "0";
                                }
                                desTxtUnit3 = Convert.ToDecimal(TxtUnit3.Text);
                            }
                            if (unit3 == true)
                            {
                                TextBox TxtUnit4 = (TextBox)tblDynamic.Rows[i].FindControl("txtUnitThird" + i.ToString());
                                if (TxtUnit4.Text == "")
                                {
                                    TxtUnit4.Text = "0";
                                }
                                desTxtUnit4 = Convert.ToDecimal(TxtUnit4.Text);
                            }

                            SqlParameter[] insChild = 
                                                    {
                                                        new SqlParameter("@CUSTOMERPRECHILDID","0"),
                                                        new SqlParameter("@CUSTOMERPREID",strParentId),
                                                        new SqlParameter("@SERVICEID",ddlservice.SelectedValue), 
                                                        new SqlParameter("@RATE",desTxtRate),
                                                        new SqlParameter("@UNIT",desTxtUnit),
                                                        new SqlParameter("@UNIT2",desTxtUnit2),
                                                        new SqlParameter("@UNIT3",desTxtUnit3),
                                                        new SqlParameter("@UNIT4",desTxtUnit4),
                                                        new SqlParameter("@UOM",strTxtUOM)
                                                        };

                            du.executeprocedure("USP_IVC_SaveCustomerPreferenceServices", insChild);
                        }
                        scope.Complete();
                    }

                    Response.Redirect("~\\Main\\ServiceInvoice\\CustomerPreference.aspx?msg=IS&action=addnew");
                }
                else if (btnSave.Text == "Update")
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        updateInChild(updateInParent(ViewState["custPreId"].ToString()));
                        scope.Complete();
                        Response.Redirect("~\\Main\\ServiceInvoice\\CustomerPreference.aspx?msg=UP&action=view&id=");
                    }
                }
            }                
            
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }
    protected void grdCustPref_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "editPreference")
        {
            Response.Redirect("~\\Main\\ServiceInvoice\\CustomerPreference.aspx?msg=UP&action=addnew&id=" + e.CommandArgument.ToString());
        }
    }
    protected void grdCustPref_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCustPref.PageIndex = e.NewPageIndex;
        fillGrid();
    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        if (btnSave.Text == "Update")
        {
            Response.Redirect("~/main/ServiceInvoice/CustomerPreference.aspx?action=view");
        }
        else
        {
            Response.Redirect("~/Welcome.aspx");
        }
    }
    protected void grdCustPref_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string strDataKeyCustomer = grdCustPref.DataKeys[e.Row.RowIndex].Values["CUSTOMERPREID"].ToString();
            string strDataKeyFormat = grdCustPref.DataKeys[e.Row.RowIndex].Values["FORMATID"].ToString();
            DataTable DTBind = CreateTableForDetail(strDataKeyCustomer, strDataKeyFormat);
            GridView grdDetails = (GridView)e.Row.FindControl("grdDetails");
            grdDetails.DataSource = DTBind;
            grdDetails.DataBind();
        }
    }
    protected void grdDetails_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Text = "Service Name";
        }
    }
    protected void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCustomer.SelectedIndex > 0 && ddlBillFormat.SelectedIndex > 0)
        {
            string strsql = "select customerpreid,customerledgerid,formatid from IVC_CUSTOMER_PREFERENCE_PARENT where customerledgerid='" + ddlCustomer.SelectedValue + "'and  formatid='" + ddlBillFormat.SelectedValue + "'";
            DataTable dtCheckPre = du.getdata(strsql);
            if (dtCheckPre.Rows.Count == 0)
            {
                tblDynamic.Rows.Clear();
                ControlCounter = new List<string>();
                //ButtonID = new List<string>();
                //ViewState["ButtonID"] = ButtonID;

                ServiceID = new List<string>();
                ViewState["ServiceID"] = ServiceID;

                RateID = new List<string>();
                ViewState["RateID"] = RateID;

                UnitID = new List<string>();
                ViewState["UnitID"] = UnitID;

                Unit1ID = new List<string>();
                ViewState["Unit1ID"] = Unit1ID;

                Unit2ID = new List<string>();
                ViewState["Unit2ID"] = Unit2ID;

                Unit3ID = new List<string>();
                ViewState["Unit3ID"] = Unit3ID;

                UOMID = new List<string>();
                ViewState["UOMID"] = UOMID;
                ViewState["formatid"] = ddlBillFormat.SelectedValue.ToString();

                rdcount = 0;
                rowcount = 0;
                fillViewStateforRowCreate();
                AddRow();
                btnSave.Text = "Save";
            }
            else
            {
                CheckPreAddedORUpdateNew(dtCheckPre.Rows[0]["customerpreid"].ToString());
                ViewState["custPreId"] = dtCheckPre.Rows[0]["customerpreid"].ToString();
                btnSave.Text = "Update";
            }
        }
    }
}